using namespace std;

#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>

static const int MAX_WEIGHT = 255;

inline void usage() {
    cout << "Graph cut with minimal weight" << endl;
    cout << "Usage: a.out <filename>" << endl;
    cout << "Filename should be in a format made by graph generator." << endl;
}

int getRandomWeight() {
    return rand() % MAX_WEIGHT + 1;
}

/*
 * Short script for assigning weights to all edges of a given graph.
 */
int main(int argc, char** argv) {

    srand(time(NULL));

    // no src file appended
    if (argc < 2) {
        usage();
        return 1;
    }

    // nonexistent src file
    fstream in;
    in.open(argv[1]);
    if (!in || in.eof()) {
        cerr << "Empty or no file" << endl;
        return 1;
    }

    int nc = 0, i, j, totalSize, random, value;
    char c;
    
    in >> nc;

    totalSize = nc*nc;
    int ** incidenceMatrix = new int * [nc];
    for (i=0; i < nc; i++) { // allocate rows
        incidenceMatrix[i] = new int [nc];
    }


    for (i = 0; i < totalSize; i++) { // coords: i/nc, i%nc
        in >> c;
        c -= '0'; /// char to int retype :-)
        if (i / nc > i % nc) continue; // skip values under main diagonal (they are the same as those above)

        random = getRandomWeight();
        value = c*random;
        incidenceMatrix[i / nc][i % nc] = value;
        incidenceMatrix[i % nc][i / nc] = value;
    }

    // output
    cout << nc << endl;
    for (i = 0; i < nc; i++) {
        for (j = 0; j < nc; j++) {
            cout << incidenceMatrix[i][j] << ' ';
        }
        cout << endl;
    }

    // free memory
    for (i=0; i < nc; i++) {
        delete [] incidenceMatrix[i];
    }
    
    delete [] incidenceMatrix;

    return 0;
}

